# Load libraries ----
library(tidyverse)
library(ggthemes)
library(multcomp)
library(gtsummary)
library(effects)
library(ggeffects)
library(gridExtra)
library(cowplot)
library(grid)
library(haven)
library(srvyr)
library(Hmisc)
library(rio)
library(modelsummary)
library(jtools)
library(xtable)
library(ggpubr)
library(rstatix)
library(interactions)
library(stevemisc)
library(lmtest)
library(sandwich)
library(estimatr)
library(dplyr)
library(kableExtra)
library(flextable)

options(scipen = 999) # Turn off scientific notation

# Define themes ----
mytheme <- function(...){
  theme_clean() +   
    theme(plot.title.position="panel",
          text=element_text(family="serif", color="gray15"), 
          axis.title=element_text(size=12, color="gray15"), 
          axis.text=element_text(size=12, color="gray15"),
          plot.title = element_text(size=12, color = "gray15"), 
          plot.caption=element_text(size = 12),  
          legend.position = "bottom",
          plot.background = element_rect(color="white"), 
          axis.title.y = element_text(margin = margin(t=0,r=10,b=0,l=0)), 
          legend.background = element_rect(color=NA), 
          axis.ticks = element_line(color="gray15"), 
          axis.line.x = element_line(color="gray15"), 
          axis.line.y = element_line(color="gray15"),
          panel.grid.major.y = element_line(color = "grey90", linetype = "solid"),
          legend.text = element_text(size = 16),
          strip.text.x = element_text(size = 18),
          axis.title.x = element_blank())
  }

mytheme2 <- function(...){
  theme_clean() +   
    theme(plot.title.position="panel",
          text=element_text(family="serif", color="gray15"), 
          axis.title=element_text(size=18, color="gray15"), 
          axis.text=element_text(size=12, color="gray15"),
          plot.title = element_text(size=18, color = "gray15"), 
          plot.caption=element_text(size = 12),  
          legend.position = "bottom",
          plot.background = element_rect(color="white"), 
          axis.title.y = element_text(margin = margin(t=0,r=10,b=0,l=0)), 
          legend.background = element_rect(color=NA), 
          axis.ticks = element_line(color="gray15"), 
          axis.line.x = element_line(color="gray15"), 
          axis.line.y = element_line(color="gray15"),
          panel.grid.major.y = element_line(color = "grey90", linetype = "solid"),
          strip.text.x = element_text(size = 18),
          legend.text = element_text(size = 16))
  }

mytheme3 <- function(...){
  theme_clean() +   
    theme(plot.title.position="panel",
          text=element_text(family="serif", color="gray15"), 
          axis.title=element_text(size=12, color="gray15"), 
          axis.text.y=element_text(size=12, color="gray15"),
          axis.text.x = element_text(size=12, color="gray15"),
          plot.title = element_text(size=12, color = "gray15"), 
          plot.caption=element_text(size = 10),  
          plot.background = element_rect(color="white"), 
          axis.title.x = element_text(margin = margin(t=0,r=10,b=0,l=0)), 
          axis.ticks = element_line(color="gray15"), 
          axis.line.x = element_line(color="gray15"), 
          axis.line.y = element_line(color="gray15"),
          panel.grid.major.x = element_line(color = "grey90", linetype = "solid"),
          panel.grid.major.y = element_line(color = "white", linetype = "solid"),
          #legend.position = "bottom",
          legend.text = element_text(size = 12),
          legend.title= element_blank(),
          legend.background = element_rect(color=NA),
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-10,-10,0,-10),
          strip.text.x = element_text(size = 10),
          axis.title.y = element_blank())
  }


# Load in data ----
dso = import("Exp.1.data.csv")

ds <- dso %>%
  filter(consent == "I agree to participate", 
         attention == "Slightly interested",
         Finished == "TRUE",
         rid != '') %>%
  mutate(
    project_assignment = case_when(
      PPExperiment_DO == "transportation|post_feeling|post_effective|post_represent|post_responsible|post_approval" ~ "transportation",
      PPExperiment_DO == "public_lands|post_feeling|post_effective|post_represent|post_responsible|post_approval" ~ "public_lands",
      PPExperiment_DO == "employment|post_feeling|post_effective|post_represent|post_responsible|post_approval" ~ "employment",
      PPExperiment_DO == "defense|post_feeling|post_effective|post_represent|post_responsible|post_approval" ~ "defense",
      PPExperiment_DO == "public_safety|post_feeling|post_effective|post_represent|post_responsible|post_approval" ~ "public_safety",
      PPExperiment_DO == "local_economy|post_feeling|post_effective|post_represent|post_responsible|post_approval" ~ "local_economy",
      PPExperiment_DO == "healthcare|post_feeling|post_effective|post_represent|post_responsible|post_approval" ~ "healthcare",
      PPExperiment_DO == "education|post_feeling|post_effective|post_represent|post_responsible|post_approval" ~ "education",
      PPExperiment_DO == "environment|post_feeling|post_effective|post_represent|post_responsible|post_approval" ~ "environment"
    ),
    
    education_rank = issue_ranking_6,
    employment_rank = issue_ranking_7,
    defense_rank = issue_ranking_10,
    healthcare_rank = issue_ranking_9,
    public_safety_rank = issue_ranking_4,
    public_lands_rank = issue_ranking_3,
    evironment_rank = issue_ranking_5,
    economy_rank = issue_ranking_11,
    transportation_rank = issue_ranking_2,
    
    education_therm = case_when(education_rank == 1 ~ spending_therm_1,
                                education_rank == 2 ~ spending_therm_2,
                                education_rank == 3 ~ spending_therm_3,
                                education_rank == 4 ~ spending_therm_4,
                                education_rank == 5 ~ spending_therm_5,
                                education_rank == 6 ~ spending_therm_6,
                                education_rank == 7 ~ spending_therm_7,
                                education_rank == 8 ~ spending_therm_8,
                                education_rank == 9 ~ spending_therm_9),
    employment_therm = case_when(employment_rank == 1 ~ spending_therm_1,
                                 employment_rank == 2 ~ spending_therm_2,
                                 employment_rank == 3 ~ spending_therm_3,
                                 employment_rank == 4 ~ spending_therm_4,
                                 employment_rank == 5 ~ spending_therm_5,
                                 employment_rank == 6 ~ spending_therm_6,
                                 employment_rank == 7 ~ spending_therm_7,
                                 employment_rank == 8 ~ spending_therm_8,
                                 employment_rank == 9 ~ spending_therm_9),
    defense_therm = case_when(defense_rank == 1 ~ spending_therm_1,
                              defense_rank == 2 ~ spending_therm_2,
                              defense_rank == 3 ~ spending_therm_3,
                              defense_rank == 4 ~ spending_therm_4,
                              defense_rank == 5 ~ spending_therm_5,
                              defense_rank == 6 ~ spending_therm_6,
                              defense_rank == 7 ~ spending_therm_7,
                              defense_rank == 8 ~ spending_therm_8,
                              defense_rank == 9 ~ spending_therm_9),
    healthcare_therm = case_when(healthcare_rank == 1 ~ spending_therm_1,
                                 healthcare_rank == 2 ~ spending_therm_2,
                                 healthcare_rank == 3 ~ spending_therm_3,
                                 healthcare_rank == 4 ~ spending_therm_4,
                                 healthcare_rank == 5 ~ spending_therm_5,
                                 healthcare_rank == 6 ~ spending_therm_6,
                                 healthcare_rank == 7 ~ spending_therm_7,
                                 healthcare_rank == 8 ~ spending_therm_8,
                                 healthcare_rank == 9 ~ spending_therm_9),
    public_safety_therm = case_when(public_safety_rank == 1 ~ spending_therm_1,
                                    public_safety_rank == 2 ~ spending_therm_2,
                                    public_safety_rank == 3 ~ spending_therm_3,
                                    public_safety_rank == 4 ~ spending_therm_4,
                                    public_safety_rank == 5 ~ spending_therm_5,
                                    public_safety_rank == 6 ~ spending_therm_6,
                                    public_safety_rank == 7 ~ spending_therm_7,
                                    public_safety_rank == 8 ~ spending_therm_8,
                                    public_safety_rank == 9 ~ spending_therm_9),
    public_lands_therm = case_when(public_lands_rank == 1 ~ spending_therm_1,
                                   public_lands_rank == 2 ~ spending_therm_2,
                                   public_lands_rank == 3 ~ spending_therm_3,
                                   public_lands_rank == 4 ~ spending_therm_4,
                                   public_lands_rank == 5 ~ spending_therm_5,
                                   public_lands_rank == 6 ~ spending_therm_6,
                                   public_lands_rank == 7 ~ spending_therm_7,
                                   public_lands_rank == 8 ~ spending_therm_8,
                                   public_lands_rank == 9 ~ spending_therm_9),
    evironment_therm = case_when(evironment_rank == 1 ~ spending_therm_1,
                                 evironment_rank == 2 ~ spending_therm_2,
                                 evironment_rank == 3 ~ spending_therm_3,
                                 evironment_rank == 4 ~ spending_therm_4,
                                 evironment_rank == 5 ~ spending_therm_5,
                                 evironment_rank == 6 ~ spending_therm_6,
                                 evironment_rank == 7 ~ spending_therm_7,
                                 evironment_rank == 8 ~ spending_therm_8,
                                 evironment_rank == 9 ~ spending_therm_9),
    economy_therm = case_when(economy_rank == 1 ~ spending_therm_1,
                              economy_rank == 2 ~ spending_therm_2,
                              economy_rank == 3 ~ spending_therm_3,
                              economy_rank == 4 ~ spending_therm_4,
                              economy_rank == 5 ~ spending_therm_5,
                              economy_rank == 6 ~ spending_therm_6,
                              economy_rank == 7 ~ spending_therm_7,
                              economy_rank == 8 ~ spending_therm_8,
                              economy_rank == 9 ~ spending_therm_9),
    transportation_therm = case_when(transportation_rank == 1 ~ spending_therm_1,
                                     transportation_rank == 2 ~ spending_therm_2,
                                     transportation_rank == 3 ~ spending_therm_3,
                                     transportation_rank == 4 ~ spending_therm_4,
                                     transportation_rank == 5 ~ spending_therm_5,
                                     transportation_rank == 6 ~ spending_therm_6,
                                     transportation_rank == 7 ~ spending_therm_7,
                                     transportation_rank == 8 ~ spending_therm_8,
                                     transportation_rank == 9 ~ spending_therm_9),
    
    project_rank = case_when(
      project_assignment == "transportation" ~ transportation_rank,
      project_assignment == "public_lands" ~ public_lands_rank,
      project_assignment == "employment" ~ employment_rank,
      project_assignment == "defense" ~ defense_rank,
      project_assignment == "public_safety" ~ public_safety_rank,
      project_assignment == "local_economy" ~ economy_rank,
      project_assignment == "healthcare" ~ healthcare_rank,
      project_assignment == "education" ~ education_rank,
      project_assignment == "environment" ~ evironment_rank
    ),
    
    project_rank_cat = case_when(
      project_rank %in% 1:3 ~ "high",
      project_rank %in% 4:6 ~ "mid",
      project_rank %in% 7:9 ~ "low"
    ),
    project_rank_cat = factor(project_rank_cat, levels = c("low", "mid", "high")),
    
    project_therm = case_when(
      project_assignment == "transportation" ~ transportation_therm,
      project_assignment == "public_lands" ~ public_lands_therm,
      project_assignment == "employment" ~ employment_therm,
      project_assignment == "defense" ~ defense_therm,
      project_assignment == "public_safety" ~ public_safety_therm,
      project_assignment == "local_economy" ~ economy_therm,
      project_assignment == "healthcare" ~ healthcare_therm,
      project_assignment == "education" ~ education_therm,
      project_assignment == "environment" ~ evironment_therm
    ),
    
    pre_feeling = feeling_grid_2,
    feeling_change = post_feeling_34 - pre_feeling,
    
    partyr = case_when(
      party %in% c("Not very strong Democrat", "Strong Democrat") ~ "dem",
      party %in% c("Not very strong Republican", "Strong Republican") ~ "rep",
      party == "Independent" ~ "ind"
    )
  ) %>%
  select(
    project_assignment,
    education_rank, employment_rank, defense_rank, healthcare_rank,
    public_safety_rank, public_lands_rank, evironment_rank, economy_rank, transportation_rank,
    education_therm, employment_therm, defense_therm, healthcare_therm, public_safety_therm,
    public_lands_therm, evironment_therm, economy_therm, transportation_therm,
    project_rank, project_rank_cat, project_therm,
    pre_feeling, feeling_change, partyr
  )

# Manuscript Figure 3 ----

ds = ds %>% 
  mutate(education_rank = revcode(education_rank),
         employment_rank = revcode(employment_rank),
         defense_rank = revcode(defense_rank),
         healthcare_rank = revcode(healthcare_rank),
         public_safety_rank = revcode(public_safety_rank),
         public_lands_rank = revcode(public_lands_rank),
         evironment_rank = revcode(evironment_rank),
         economy_rank = revcode(economy_rank),
         transportation_rank = revcode(transportation_rank))

ds %>% 
  select(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, evironment_rank, economy_rank, transportation_rank) %>% 
  pivot_longer(
    cols = c(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, evironment_rank, economy_rank, transportation_rank),
    cols_vary = "fastest",
    names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = mean(value), 
            se = sd(value)/sqrt(nrow),
            ci = 1.96*se) %>% 
  mutate(type = fct_reorder(type, mean)) %>%
  ggplot(., aes(x = type, y = mean)) + 
  geom_pointrange(aes(ymin = mean-ci, ymax = mean+ci), position = position_dodge(width = .5), size = 0.25, linewidth=0.5) + 
  labs(shape = "",
       y = "Mean Ranking Score (1-9)") +  
  scale_x_discrete(labels=c("education_rank" = "Education", "employment_rank" = "Employement", "defense_rank" = "Defense", "healthcare_rank" = "Healthcare", "public_safety_rank" = "Public Safety", "public_lands_rank" = "Public Lands", "evironment_rank" = "Environment", "economy_rank" = "Local Economy", "transportation_rank" = "Transportation")) +
  geom_vline(xintercept=c(0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5),color="lightgrey", linetype = "dashed")+
  coord_flip()+
  mytheme3() -> figure_3

# Manuscript Figure 4 ----

# Figure 4A
ds %>% 
  drop_na(feeling_change, project_rank_cat) %>%
  group_by(project_rank_cat) %>% 
  summarise(nrow = length(feeling_change), 
            mean = mean(feeling_change), 
            se = sd(feeling_change)/sqrt(nrow),
            ci = 1.96*se) %>% 
  ggplot(., aes(x = project_rank_cat, y = mean)) + 
  geom_pointrange(aes(ymin = mean-ci, ymax = mean+ci), position = position_dodge(width = .5), size = 0.75, linewidth=1) + 
  labs(shape = "",
       title = "A) Assigned Project Ranking Category",
       y = "Senator Feeling Therm. Change") +  
  scale_x_discrete(labels=c("high" = "High \n Priority", "mid" = "Medium \n Priority", "low" = "Low \n Priority")) +
  scale_y_continuous(limits=c(-5.75,16.75), breaks = c(-5, 0, 5, 10, 15)) +
  mytheme() -> figure_4a

# Figure 4B
ds %>% 
  drop_na(feeling_change, project_therm) %>%
  ggplot(aes(x = project_therm, y = feeling_change)) +
  geom_smooth(method = 'loess') +
  labs(title = "B) Assigned Project Rating (0-100)",
       y = "") +
  scale_x_continuous(breaks = c(0, 25, 50, 75, 100), label = c("0\n ", "25\n ", "50\nAssigned Project Rating (0-100)","75\n ","100\n "))+
  mytheme() -> figure_4b

# Combine
plot_grid(figure_4a,figure_4b) -> figure_4


# Manuscript Figure 5 ----
partythree.labs <- c("Democrats", "Independents","Republicans")
names(partythree.labs) <- c("dem","ind","rep")

ds %>% 
  drop_na(feeling_change, project_therm, partyr) %>%
  ggplot(aes(x = project_therm, y = feeling_change, color = partyr, fill = partyr)) +
  geom_smooth(method = 'loess') +
  labs(x = "Assigned Project Rating (0-100)",
       y = "Senator Feeling Therm. Change") +  
  scale_color_manual(values=c("mediumblue","green4","red3"), guide = 'none')+
  scale_fill_manual(values=c("mediumblue","green4","red3"), guide = 'none')+
  mytheme2() + facet_grid(~partyr, labeller = labeller(partyr = partythree.labs)) -> figure_5
